require "Common/define"
--血条控制页面
UIBloodBar = {};
HelperFunc.SetUIEnv(UIBloodBar)
local this = UIBloodBar;
this.barPool = {};
this.poolNode = nil;
this.hasInit = nil;
this.camera = nil

this.idle_fly = {}
this.fly_item = {} --如果动画没有回调，此表确保，战斗结束后，跳字都能回收
function UIBloodBar.Init()
    local name, package = getUIName(PanelNames.UIBloodBar);
    panelMgr:ShowUI(name, package, this.OnCreate, Config.useLocalUI);
end

--定义控件
function UIBloodBar.OnCreate(obj)
    if not hasInit then
        gameObject = obj;
        poolNode = obj:GetChild("pool");
    end
    hasInit = true;
    this.idle_fly = {}
    this.fly_item = {}
    ListenEvent(Config.EventType.Fight_Finish, this.RemoveAllTextFly)
    ListenEvent(Config.EventType.Fight_Finish, this.ClearBloodBar)
    --this.LocalListenEvent();
end

function UIBloodBar.Update()
end

function UIBloodBar.OnDestroy()
    hasInit = false;
    this.idle_fly = {}
    this.fly_item = {}
    CloseEvent(Config.EventType.Fight_Finish, this.RemoveAllTextFly)
    CloseEvent(Config.EventType.Fight_Finish, this.ClearBloodBar)

    --UpdateBeat:Remove(this.Update, self);
    --this.LocalCloseEvent();
end

function UIBloodBar.GetTextMesh()
    local item = nil
    if gameObject.visible == false then
        gameObject.visible = true
    end
    if next(this.idle_fly) == nil then
        --if Name == Config.NORMAL_HURTS then
        --
        --elseif Name == Config.NORMAL_CRIT then
        --
        --elseif Name == Config.NORMAL_DODGE then
        --
        --elseif Name == Config.NORMAL_IMMUNITY then
        --
        --elseif Name == Config.NORMAL_HEAL then
        --
        --elseif Name == Config.NORMAL_SP then
        --
        --elseif Name == Config.NORMAL_SHIELDS then
        --
        --end
        item = newGComponent("FlyTest", "BattleItems")
        item.gameObjectName = "FlyTest";
    else
        item = this.idle_fly[1]
        table.remove(this.idle_fly, 1)
    end
    gameObject:AddChild(item);
    table.insert(this.fly_item, item)
    return item;
end
--function UIBloodBar.LocalListenEvent()
--    ListenEvent(Config.EventType.Fresh_FPS, this.Fresh_FPS);
--end
--
--function UIBloodBar.LocalCloseEvent()
--    CloseEvent(Config.EventType.Fresh_FPS, this.Fresh_FPS);
--end

function UIBloodBar.GetBloodBar()
    if UIBloodBar.poolNode == nil then
        UIBloodBar.Init()
    end
    if UIBloodBar.gameObject.visible == false then
        UIBloodBar.gameObject.visible = true
    end
    local bloodBar = UIBloodBar.poolNode:GetChild("bloodBar");
    if bloodBar == nil then
        bloodBar = newGComponent("Headbar", "BattleItems");
        bloodBar.gameObjectName = "bloodBar";
        bloodBar.visible = false
    end
    UIBloodBar.gameObject:AddChild(bloodBar);
    return bloodBar;
end

function UIBloodBar.RemoveBloodBar(item)
    poolNode:AddChild(item);
end

function UIBloodBar.RemoveBloodFly(item)
    poolNode:AddChild(item)
    table.insert(this.idle_fly, item)
    if table.contains(this.fly_item, item) then
        table.removeByValue(this.fly_item, item)
    end
end
function UIBloodBar.RemoveAllTextFly()
    for k, v in pairs(this.fly_item) do
        poolNode:AddChild(v)
        table.insert(this.idle_fly, v)
    end
    table.clear(this.fly_item)
    while UIBloodBar.gameObject:GetChild("FlyTest") do
        local item = UIBloodBar.gameObject:GetChild("FlyTest");
        if item then
            poolNode:AddChild(item)
        end
    end

end

function UIBloodBar.ClearBloodBar()
    while UIBloodBar.gameObject:GetChild("bloodBar") do
        local item = UIBloodBar.gameObject:GetChild("bloodBar");
        if item then
            poolNode:AddChild(item)
        end
    end
    UIBloodBar.gameObject.visible = false;
end
return UIBloodBar;